﻿namespace AnyOffice.DAL
{
    using AnyOffice.Model;
    using Microsoft.ApplicationBlocks.Data;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class RmsRoomDao
    {
        public bool Delete(SqlConnection conn, int roomId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("DELETE FROM [dbo].[Rms_Lend] ");
            builder.Append("WHERE ");
            builder.Append("\t[FileId] IN ");
            builder.Append("\t\t( ");
            builder.Append("\t\t\tSELECT [FileId] ");
            builder.Append("\t\t\tFROM ");
            builder.Append("\t\t\t\t[dbo].[Rms_File] ");
            builder.Append("\t\t\tWHERE ");
            builder.Append("\t\t\t\t[RollId] IN (SELECT [RollId] FROM [dbo].[Rms_Roll] WHERE [RoomId] = @RoomId) ");
            builder.Append("\t\t) ");
            builder.Append("UPDATE [dbo].[Attachment] ");
            builder.Append("SET ");
            builder.Append("\t[DeleteFlag] = 1 ");
            builder.Append("WHERE ");
            builder.Append("\t[AttachmentId] IN ( ");
            builder.Append("\t\t\t\tSELECT [AttachmentId] ");
            builder.Append("\t\t\t\tFROM ");
            builder.Append("\t\t\t\t\t[dbo].[Rms_File] ");
            builder.Append("\t\t\t\tWHERE ");
            builder.Append("\t\t\t\t\t[RollId] IN (SELECT [RollId] FROM [dbo].[Rms_Roll] WHERE [RoomId] = @RoomId) AND ISNULL([AttachmentId] ,'') <>'') ");
            builder.Append("DELETE FROM [dbo].[Rms_File] ");
            builder.Append("WHERE ");
            builder.Append("\t[RollId] IN (SELECT [RollId] FROM [dbo].[Rms_Roll] WHERE [RoomId] = @RoomId) ");
            builder.Append("DELETE FROM [dbo].[Rms_Roll] ");
            builder.Append("WHERE ");
            builder.Append("\t[RollId] IN (SELECT [RollId] FROM [dbo].[Rms_Roll] WHERE [RoomId] = @RoomId) ");
            builder.Append("DELETE FROM [dbo].[Rms_Room] ");
            builder.Append("WHERE ");
            builder.Append("\t[RoomId] = @RoomId ");
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@RoomId", SqlDbType.Int) };
            commandParameters[0].Value = roomId;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, builder.ToString(), commandParameters);
            return true;
        }

        public int Insert(SqlConnection conn, RmsRoomData data)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("INSERT INTO [dbo].[Rms_Room] ");
            builder.Append("( ");
            builder.Append("\t[RoomCode], ");
            builder.Append("\t[RoomName], ");
            builder.Append("\t[DeptId], ");
            builder.Append("\t[Remark], ");
            builder.Append("\t[CategoryNo], ");
            builder.Append("\t[CatalogNo], ");
            builder.Append("\t[ManageUser], ");
            builder.Append("\t[AddUser], ");
            builder.Append("\t[AddTime], ");
            builder.Append("\t[ModUser], ");
            builder.Append("\t[ModTime], ");
            builder.Append("\t[DelUser], ");
            builder.Append("\t[DelTime] ");
            builder.Append(") ");
            builder.Append("VALUES ");
            builder.Append("( ");
            builder.Append("\t@RoomCode, ");
            builder.Append("\t@RoomName, ");
            builder.Append("\t@DeptId, ");
            builder.Append("\t@Remark, ");
            builder.Append("\t@CategoryNo, ");
            builder.Append("\t@CatalogNo, ");
            builder.Append("\t@ManageUser, ");
            builder.Append("\t@AddUser, ");
            builder.Append("\t@AddTime, ");
            builder.Append("\t@ModUser, ");
            builder.Append("\t@ModTime, ");
            builder.Append("\t@DelUser, ");
            builder.Append("\t@DelTime ");
            builder.Append(") ");
            builder.Append("SELECT SCOPE_IDENTITY() AS RetValue ");
            SqlParameter[] commandParameters = new SqlParameter[13];
            commandParameters[0] = new SqlParameter("@RoomCode", SqlDbType.NVarChar, 200);
            commandParameters[0].Value = data.RoomCode;
            commandParameters[1] = new SqlParameter("@RoomName", SqlDbType.NVarChar, 200);
            commandParameters[1].Value = data.RoomName;
            commandParameters[2] = new SqlParameter("@DeptId", SqlDbType.Int);
            if (data.DeptIdIsNull)
            {
                commandParameters[2].Value = DBNull.Value;
            }
            else
            {
                commandParameters[2].Value = data.DeptId;
            }
            commandParameters[3] = new SqlParameter("@Remark", SqlDbType.NText);
            if (data.RemarkIsNull)
            {
                commandParameters[3].Value = DBNull.Value;
            }
            else
            {
                commandParameters[3].Value = data.Remark;
            }
            commandParameters[4] = new SqlParameter("@CategoryNo", SqlDbType.NVarChar, 100);
            if (data.CategoryNoIsNull)
            {
                commandParameters[4].Value = DBNull.Value;
            }
            else
            {
                commandParameters[4].Value = data.CategoryNo;
            }
            commandParameters[5] = new SqlParameter("@CatalogNo", SqlDbType.NVarChar, 100);
            if (data.CatalogNoIsNull)
            {
                commandParameters[5].Value = DBNull.Value;
            }
            else
            {
                commandParameters[5].Value = data.CatalogNo;
            }
            commandParameters[6] = new SqlParameter("@ManageUser", SqlDbType.NText);
            if (data.ManageUserIsNull)
            {
                commandParameters[6].Value = DBNull.Value;
            }
            else
            {
                commandParameters[6].Value = data.ManageUser;
            }
            commandParameters[7] = new SqlParameter("@AddUser", SqlDbType.NVarChar, 20);
            commandParameters[7].Value = data.AddUser;
            commandParameters[8] = new SqlParameter("@AddTime", SqlDbType.DateTime);
            commandParameters[8].Value = data.AddTime;
            commandParameters[9] = new SqlParameter("@ModUser", SqlDbType.NVarChar, 20);
            if (data.ModUserIsNull)
            {
                commandParameters[9].Value = DBNull.Value;
            }
            else
            {
                commandParameters[9].Value = data.ModUser;
            }
            commandParameters[10] = new SqlParameter("@ModTime", SqlDbType.DateTime);
            if (data.ModTimeIsNull)
            {
                commandParameters[10].Value = DBNull.Value;
            }
            else
            {
                commandParameters[10].Value = data.ModTime;
            }
            commandParameters[11] = new SqlParameter("@DelUser", SqlDbType.NVarChar, 20);
            if (data.DelUserIsNull)
            {
                commandParameters[11].Value = DBNull.Value;
            }
            else
            {
                commandParameters[11].Value = data.DelUser;
            }
            commandParameters[12] = new SqlParameter("@DelTime", SqlDbType.DateTime);
            if (data.DelTimeIsNull)
            {
                commandParameters[12].Value = DBNull.Value;
            }
            else
            {
                commandParameters[12].Value = data.DelTime;
            }
            object obj2 = SqlHelper.ExecuteScalar(conn, CommandType.Text, builder.ToString(), commandParameters);
            if (obj2 != DBNull.Value)
            {
                return Convert.ToInt32(obj2);
            }
            return -1;
        }

        public DataTable SelectAll(SqlConnection conn)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("SELECT ");
            builder.Append("\t[RoomId], ");
            builder.Append("\t[RoomName] ");
            builder.Append("FROM ");
            builder.Append("\t[dbo].[Rms_Room] ");
            builder.Append("ORDER BY ");
            builder.Append("\t[RoomId] ASC ");
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, builder.ToString());
            set.Tables[0].TableName = "Rms_Room";
            return set.Tables[0];
        }

        public DataTable SelectByRoomId(SqlConnection conn, int roomId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("SELECT ");
            builder.Append("\t* ");
            builder.Append("FROM ");
            builder.Append("\t[dbo].[Rms_Room] ");
            builder.Append("WHERE ");
            builder.Append("\t[RoomId] = @RoomId ");
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@RoomId", SqlDbType.Int) };
            commandParameters[0].Value = roomId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, builder.ToString(), commandParameters);
            set.Tables[0].TableName = "Rms_Room";
            return set.Tables[0];
        }

        public DataTable SelectPaged(SqlConnection conn, int pagesize, int pageindex, bool docount)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("IF(@docount=1) ");
            builder.Append("\tSELECT COUNT([RoomId]) FROM [dbo].[Rms_Room] ");
            builder.Append("ELSE ");
            builder.Append("\tBEGIN ");
            builder.Append("\t\tDECLARE @IndexTable TABLE(ID int identity(1,1),PK int) ");
            builder.Append("\t\tDECLARE @PageLowerBound int ");
            builder.Append("\t\tDECLARE @PageUpperBound int ");
            builder.Append("\t\tSET @PageLowerBound=(@pageindex-1)*@pagesize ");
            builder.Append("\t\tSET @PageUpperBound=@PageLowerBound+@pagesize ");
            builder.Append("\t\tSET rowcount @PageUpperBound ");
            builder.Append("\t\t ");
            builder.Append("\t\tINSERT INTO @IndexTable(PK) SELECT [RoomId] FROM [dbo].[Rms_Room] ORDER BY [RoomId] DESC ");
            builder.Append("\t\t ");
            builder.Append("\t\tSELECT ");
            builder.Append("\t\t\t[Rms_Room].[RoomId], ");
            builder.Append("\t\t\t[Rms_Room].[RoomCode], ");
            builder.Append("\t\t\t[Rms_Room].[RoomName], ");
            builder.Append("\t\t\t[Rms_Room].[AddTime], ");
            builder.Append("\t\t\tdbo.GetDeptNames(CAST([Rms_Room].[DeptId] AS nvarchar(20))) AS DeptName, ");
            builder.Append("\t\t\t@pageindex AS pageindex ");
            builder.Append("\t\tFROM ");
            builder.Append("\t\t\t[dbo].[Rms_Room], ");
            builder.Append("\t\t\t@IndexTable t\t\t\t ");
            builder.Append("\t\tWHERE ");
            builder.Append("\t\t\t[Rms_Room].[RoomId] = t.PK\t\t\t ");
            builder.Append("\t\t\tAND t.ID>@PageLowerBound  ");
            builder.Append("\t\t\tAND t.ID<=@PageUpperBound ");
            builder.Append("\t\tORDER BY ");
            builder.Append("\t\t\t[RoomId] DESC ");
            builder.Append("\tEND ");
            SqlParameter[] commandParameters = new SqlParameter[3];
            commandParameters[0] = new SqlParameter("@pagesize", SqlDbType.Int);
            commandParameters[0].Value = pagesize;
            commandParameters[1] = new SqlParameter("@pageindex", SqlDbType.Int);
            commandParameters[1].Value = pageindex;
            commandParameters[2] = new SqlParameter("@docount", SqlDbType.Bit);
            commandParameters[2].Value = docount;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, builder.ToString(), commandParameters);
            set.Tables[0].TableName = "Rms_Room";
            return set.Tables[0];
        }

        public bool Update(SqlConnection conn, RmsRoomData data)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("UPDATE [dbo].[Rms_Room] ");
            builder.Append("SET ");
            builder.Append("\t[RoomCode] = @RoomCode, ");
            builder.Append("\t[RoomName] = @RoomName, ");
            builder.Append("\t[DeptId] = @DeptId, ");
            builder.Append("\t[Remark] = @Remark, ");
            builder.Append("\t[ModUser] = @ModUser, ");
            builder.Append("\t[ModTime] = @ModTime ");
            builder.Append("WHERE ");
            builder.Append("\t[RoomId] = @RoomId ");
            SqlParameter[] commandParameters = new SqlParameter[14];
            commandParameters[0] = new SqlParameter("@RoomId", SqlDbType.Int);
            commandParameters[0].Value = data.RoomId;
            commandParameters[1] = new SqlParameter("@RoomCode", SqlDbType.NVarChar, 200);
            commandParameters[1].Value = data.RoomCode;
            commandParameters[2] = new SqlParameter("@RoomName", SqlDbType.NVarChar, 200);
            commandParameters[2].Value = data.RoomName;
            commandParameters[3] = new SqlParameter("@DeptId", SqlDbType.Int);
            if (data.DeptIdIsNull)
            {
                commandParameters[3].Value = DBNull.Value;
            }
            else
            {
                commandParameters[3].Value = data.DeptId;
            }
            commandParameters[4] = new SqlParameter("@Remark", SqlDbType.NText);
            if (data.RemarkIsNull)
            {
                commandParameters[4].Value = DBNull.Value;
            }
            else
            {
                commandParameters[4].Value = data.Remark;
            }
            commandParameters[5] = new SqlParameter("@CategoryNo", SqlDbType.NVarChar, 100);
            if (data.CategoryNoIsNull)
            {
                commandParameters[5].Value = DBNull.Value;
            }
            else
            {
                commandParameters[5].Value = data.CategoryNo;
            }
            commandParameters[6] = new SqlParameter("@CatalogNo", SqlDbType.NVarChar, 100);
            if (data.CatalogNoIsNull)
            {
                commandParameters[6].Value = DBNull.Value;
            }
            else
            {
                commandParameters[6].Value = data.CatalogNo;
            }
            commandParameters[7] = new SqlParameter("@ManageUser", SqlDbType.NText);
            if (data.ManageUserIsNull)
            {
                commandParameters[7].Value = DBNull.Value;
            }
            else
            {
                commandParameters[7].Value = data.ManageUser;
            }
            commandParameters[8] = new SqlParameter("@AddUser", SqlDbType.NVarChar, 20);
            commandParameters[8].Value = data.AddUser;
            commandParameters[9] = new SqlParameter("@AddTime", SqlDbType.DateTime);
            commandParameters[9].Value = data.AddTime;
            commandParameters[10] = new SqlParameter("@ModUser", SqlDbType.NVarChar, 20);
            commandParameters[10].Value = data.ModUser;
            commandParameters[11] = new SqlParameter("@ModTime", SqlDbType.DateTime);
            if (data.ModTimeIsNull)
            {
                commandParameters[11].Value = DBNull.Value;
            }
            else
            {
                commandParameters[11].Value = data.ModTime;
            }
            commandParameters[12] = new SqlParameter("@DelUser", SqlDbType.NVarChar, 20);
            if (data.DelUserIsNull)
            {
                commandParameters[12].Value = DBNull.Value;
            }
            else
            {
                commandParameters[12].Value = data.DelUser;
            }
            commandParameters[13] = new SqlParameter("@DelTime", SqlDbType.DateTime);
            if (data.DelTimeIsNull)
            {
                commandParameters[13].Value = DBNull.Value;
            }
            else
            {
                commandParameters[13].Value = data.DelTime;
            }
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, builder.ToString(), commandParameters);
            return true;
        }
    }
}

